const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules:[],
      pic2:'',
      form: {
        Group_Block_SerialNumber:'',
        Group_Block_Name:'',
        Picture_URL:'',
        Children:{ 
          group_block_detail:[{
            Group_Block_Detail_SerialNumber: '',
            Group_Block_Detail_Name: '',
            Group_Block_SerialNumber: '',
            Path:'',
            Icon:'',
            pic:''
          }] 
        }
      },
      Parent: [],
      children: '否',
      loading: false,
      isParent: true,
      isUrl:false,
      index:'',
     
    }
  },
  mounted() { 

  },
  methods: {
    submitForm(formName) {
      let _this = this
      console.log(_this.form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: `${DomainName}/getAddAction?token=${$.cookie('token')}`,
            type: 'POST',
            data: {
              Data: JSON.stringify({
                group_block_info: [_this.form]
              }),
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.history.go(-1);
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    add_system_model_mark(){
      this.form.Children.group_block_detail.push({
        Group_Block_Detail_SerialNumber: '',
        Group_Block_Detail_Name: '',
        Group_Block_SerialNumber: '',
        Path:'',
        Icon:'',
      })
      
    },
    remove_system_model_mark(){
      if (this.form.Children.group_block_detail.length>0) {
        this.form.Children.group_block_detail.pop()
      }else{
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handle(index) {
      this.index = index
      console.log(this.index);
    },
     //商品封面图片
     uploadFront(file) {
      console.log('上传第一张图片')
      let _this = this
      let num = 1
      new Compressor(file, {
        quality: num,
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.form.Children.group_block_detail[_this.index].pic = e.target.result
                if (Result.Status == "success") {
                  _this.form.Children.group_block_detail[_this.index].Icon = Result.Picture_Url
                  _this.$message.success('上传成功')
                  console.log(Result.Picture_Url);
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
     //商品封面图片
     uploadFront2(file2) {
      console.log('上传第二张图片')
      let _this = this
      let num = 1
      new Compressor(file2, {
        quality: num,
        success(file2) {
          console.log(file2.size)
          const formData = new FormData();
          formData.append('file', file2, file2.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file2);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file2.size,
                FileName: file2.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.pic2 = e.target.result
                if (Result.Status == "success") {
                  _this.form.Picture_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                  console.log(Result.Picture_Url);
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
  }
})